//
//  PersonalInfoViewController.m
//  KuWeiPark
//
//  Created by Kevin Su on 15/4/28.
//  Copyright (c) 2015年 Clief. All rights reserved.
//

#import "PersonalInfoViewController.h"
#import "PersonalHeaderCell.h"
#import "PersonalMessageCell.h"
#import "PersonalOrderCell.h"
#import "PersonalSaleTicketCell.h"
#import "AppMacro.h"
#import "SettingViewController.h"
#import "HttpClientManager.h"
#import "DataModel.h"
#import "AppDelegate.h"

@interface PersonalInfoViewController ()

@end

@implementation PersonalInfoViewController{
    
    LoginModel *loginModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.leftView.frame = CGRectMake(-1 * 0.75*self.view.frame.size.width, 0, 0.75*self.view.frame.size.width, self.view.frame.size.height);
    self.tableView.frame = CGRectMake(0, 0, self.leftView.frame.size.width, self.leftView.frame.size.height);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    [self.rightView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideLeftViewAnimation)]];
    
    loginModel = ApplicationDelegate.loginModel;
}

- (void)viewWillAppear:(BOOL)animated{
    
    [self showLeftViewAnimation];
}

- (void)showLeftViewAnimation{
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.leftView.frame = CGRectMake(0, 0, self.leftView.frame.size.width, self.view.frame.size.height);
    }];
}

- (void)hideLeftViewAnimation{
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.leftView.frame = CGRectMake(-1 * self.leftView.frame.size.width, 0, 0.75*self.view.frame.size.width, self.view.frame.size.height);
    } completion:^(BOOL finished) {
        
        [self.view removeFromSuperview];
    }];
}

#pragma mark - TableView Delegate && DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 1;
    }else if(section == 1){
        
        return 5;
    }else
        return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger section = indexPath.section;
    
    if(section == 0){
        
        return 122;
    }else{
        
        return 54;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if(section == 0){
        
        return 54;
    }else if(section == 1){
        
        return 28;
    }else
        
        return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    CGFloat height;
    if(section == 0){
        
        height = 54;
    }else if(section == 1){
        
        height = 28;
    }else
        
        height = 1;
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, height)];
    footerView.backgroundColor = RGBACOLOR(3, 11, 26, 1);
    return footerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *headerCellIdentifier = @"PersonalHeaderCell";
    NSString *bindPayCellIdentifier = @"bindPayCellIdentifier";
    NSString *messageCellIdentifier = @"PersonalMessageCell";
    NSString *orderCellIdentifier = @"PersonalOrderCell";
    NSString *saleTicketCellIdentifier = @"PersonalSaleTicketCell";
    NSString *shareCellIdentifier = @"shareCellIdentifier";
    NSString *settingCellIdentifier = @"settingCellIdentifier";
    
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    
    if(section == 0){
        
        PersonalHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:headerCellIdentifier];
        [cell initDataWithLoginModel:loginModel];
        return cell;
    }else if(section == 1){
        
        switch (row) {
            case 0:
            {
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:bindPayCellIdentifier];
                return cell;
                break;
            }
            case 1:
            {
                PersonalMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:messageCellIdentifier];
                return cell;
                break;
            }
            case 2:
            {
                PersonalOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:orderCellIdentifier];
                return cell;
                break;
            }
            case 3:
            {
                PersonalSaleTicketCell *cell = [tableView dequeueReusableCellWithIdentifier:saleTicketCellIdentifier];
                cell.leftTicketsLabel.text = [NSString stringWithFormat:@"3张"];
                return cell;
                break;
            }
            case 4:
            {
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:shareCellIdentifier];
                return cell;
                break;
            }
            default:
            {
                return nil;
                break;
            }
        }
    }else{
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:settingCellIdentifier];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //获取UIViewcontroller
    
    int index = -1; // 标志点击的那个cell
    
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    
    if(section == 0){
        
        index = 0;
    }
    if(section == 1){
        
        switch (row) {
            case 0:
            {
                index = 1;
                break;
            }
            case 1:
            {
                index = 2;
                break;
            }
            case 2:
            {
                index = 3;
                break;
            }
            case 3:
            {
                index = 4;
                break;
            }
            case 4:
            {
                index = 5;
                break;
            }
            default:
                break;
        }
    }else if(section == 2){
        
        //设置
        index = 6;
    }
    
    NSDictionary *userinfo = @{@"index":[NSString stringWithFormat:@"%i", index]};
    [[NSNotificationCenter defaultCenter] postNotificationName:Notify_ShowSelectedViewController object:nil userInfo:userinfo];
    [self hideLeftViewAnimation];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
